package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import io.netty.handler.codec.MessageAggregationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;
    @Autowired
    private CheckItemService checkItemService;
    @Autowired
    private SetmealMapper  setmealMapper;
    /**
     * 添加检查组，同时需要关联检查项
     * @param checkGroup,checkitemIds
     */
    @Override
    @Transactional
    public void add(CheckGroup checkGroup, List<Integer> checkitemIds) {
        checkGroupMapper.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        checkitemIds.forEach(checkitemId ->{
            checkGroupCheckItemMapper.add(checkGroupId,checkitemId);
        });
    }

    /**
     * 根据检查组id删除检查组
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        //判断删除的检查组是否关联套餐
        if(setmealMapper.countByCheckgroupId(id) > 0){
            throw new MessageAggregationException(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
        checkGroupCheckItemMapper.deleteByCheckGroupId(id);
        checkGroupMapper.deleteById(id);
    }

    /**
     * 根据检查组id查询检查组数据
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        //查询检查组关联的检查项
        List<Integer> checkItems = checkItemService.findChechItemIdByCheckGroupId(id);
        List<CheckItem> checkItemList = new ArrayList<>();
        checkItems.forEach(checkItemId ->{
            checkItemList.add(checkItemService.findById(checkItemId));
        });
        CheckGroup checkGroup = checkGroupMapper.findById(id);
        checkGroup.setCheckItems(checkItemList);
        return checkGroup;
    }

    /**
     *  编辑检查组，同时需要关联检查项
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    @Transactional
    public void update(CheckGroup checkGroup, List<Integer> checkitemIds) {
        checkGroupMapper.update(checkGroup);
        checkGroupCheckItemMapper.deleteByCheckGroupId(checkGroup.getId());
        checkitemIds.forEach(checkitemId ->{
            checkGroupCheckItemMapper.add(checkGroup.getId(),checkitemId);
        });
    }

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupMapper.pageQuery(queryPageBean);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 查询所有检查组
     * @return
     */
    @Override
    public List<CheckGroup> getAll() {
        List<CheckGroup> checkGroupList = checkGroupMapper.getAll();
        checkGroupList.forEach(checkGroup -> {
            Integer checkGroupId = checkGroup.getId();
            List<Integer> checkItems = checkItemService.findChechItemIdByCheckGroupId(checkGroupId);
            List<CheckItem> checkItemList = new ArrayList<>();
            checkItems.forEach(checkItemId ->{
                checkItemList.add(checkItemService.findById(checkItemId));
            });
            checkGroup.setCheckItems(checkItemList);
        });
        return checkGroupList;
    }
}
